//
//  BookMain.swift
//  GOFM
//
//  Created by siri on 2024/10/22.
//  Copyright © 2024 Glad. All rights reserved.
//

import SwiftUI

struct IdleTimeMain: View {
    
     var columns: [GridItem] = [
       GridItem(.fixed(45),spacing: 0),
       GridItem(.fixed(45),spacing: 0),
       GridItem(.fixed(45),spacing: 0),
       GridItem(.fixed(45),spacing: 0),
       GridItem(.fixed(45),spacing: 0),
       GridItem(.fixed(45),spacing: 0),
       GridItem(.fixed(45),spacing: 0)
    ]
    // 滚动索引
    @Binding  var scrollToIndex: Int?
    // 是否长按
    @State private var isPressed = false
    @State private var isLongPressArray = Array(repeating: 0, count: 112)
    var body: some View {
        HStack(alignment: .top, spacing:0) {
            VStack (spacing: 0) {
                // 星期一至星期日 文字
                ForEach(0..<16) { index in
                    Rectangle()
                        .frame(width: 50, height: 30)
                        .foregroundColor(.white)
                        .overlay(
                            Text("06:00").foregroundColor(Color(hex: "7f8182"))
                                .font(.system(size: 12))
                        )
                }
            }
            Spacer()
            ScrollViewReader { proxy in
                ScrollView(.horizontal,showsIndicators: false) {
                    HStack {
                        ForEach(0...1,id:\.self) {
                            index in
                            LazyVGrid(columns: columns,spacing: 0) {
                                ForEach(0..<112) { index in
                                    Rectangle()
                                        .fill(
                                            isLongPressArray[index]    == 0 ? Color(hex: "#89b6fa"): Color(hex: "#f2f2f6"))
                                        .frame(width: 36, height: 25)
                                        .padding(.bottom,5)
                                        .gesture(
                                            LongPressGesture(minimumDuration: 0.2,maximumDistance: 10)
                                                .onChanged { value in
                                                    print(value)
                                                    isLongPressArray[index] = 1
                                                    self.isPressed = true
                                                }
                                                .onEnded { value in
                                                    print(value)
                                                    isLongPressArray[index] = 1
                                                    self.isPressed = false
                                                }
                                        )
                                }
                            }.id(index)
                        }
                    }.background(Color.g_color_bg_white)
                }
                .frame(width: 315 ).onChange(of: scrollToIndex) { newValue in
                    if let newValue {
                        withAnimation {
                            print(newValue)
                            proxy.scrollTo(newValue, anchor: .leading)
                        }
                    }
                }
            }
        }
    }
}

//struct BookMain_Previews: PreviewProvider {
//    static var previews: some View {
//        BookMain(scrollToIndex: <#T##Binding<Int?>#>)
//    }
//}
